#include "sys.h"
#include "log.h"
#include "dial.h"
#include "cJSON.h"
#include "http_tactics.h"
#include "data_report.h"

void* do_http_tactics(void* arg)
{
    http_tactics_t* http = (http_tactics_t*)arg;
    httpTactics_t* ptr;
    struct httpTactics_head* head = &http->head;
    cJSON *result, *obj, *arr;

    dial_arg_t dial_arg = { 0 };
    dial_result_t* dial_result = NULL;
    http_result_t* http_result = NULL;
    char tmp_str[32] = { 0 };
    char topic[32] = { 0 };
    char *post_data = NULL, reportTime[32], collectTime[32];

    if (http->count == 0)
        return NULL;
    
    result = cJSON_CreateObject();
    cJSON_AddStringToObject(result, "dialTaskId", http->taskId);
    cJSON_AddStringToObject(result, "version", http->version);
    cJSON_AddStringToObject(result, "indexType", "http");
    get_timestring(reportTime);
    cJSON_AddStringToObject(result, "reportTime", reportTime);
    get_timestring(collectTime);
    cJSON_AddStringToObject(result, "collectTime", collectTime);
    // pthread_mutex_lock(&http->mutex);
    arr = cJSON_CreateArray();
    TAILQ_FOREACH(ptr, head, entry)
    {
        SPEEDTEST_WAIT();
        
        obj = cJSON_CreateObject();
        dial_arg.in.http_arg.uri = ptr->url;
        dial_arg.in.http_arg.port = (ptr->port);
        dial_arg.in.http_arg.timeout = (ptr->timeout);
        dial_arg.in.http_arg.duration = 0;
        dial_result = (dial_result_t*)dial_http_light(&dial_arg);
        http_result_print(&dial_result->out.http_result);

        http_result = &dial_result->out.http_result;
        cJSON_AddStringToObject(obj, "httpAdr", ptr->url);
        if(dial_result->err != HTTP_DIAL_SUCCESS){
            sprintf(tmp_str, "%d", ptr->timeout * 1000 + 1);
        }else{
            sprintf(tmp_str, "%.0f", http_result->latency);
        }
        cJSON_AddStringToObject(obj, "firstPageTime", tmp_str);
        sprintf(tmp_str, "%.0f", http_result->total_time);
        cJSON_AddStringToObject(obj, "downDelay", tmp_str);
        if(dial_result->err != HTTP_DIAL_SUCCESS){
            cJSON_AddNumberToObject(obj, "resultCode",  dial_result->err);
        }else{
            cJSON_AddNumberToObject(obj, "resultCode", http_result->code);
        }
        cJSON_AddNumberToObject(obj, "size", http_result->total_bytes);
        strcpy(topic, ptr->kafkaTopic);
        cJSON_AddStringToObject(obj, "businessTag", ptr->businessTag);

        get_timestring(collectTime);
        cJSON_AddStringToObject(obj, "collectTime", collectTime);

        cJSON_AddItemToArray(arr, obj);

        log_info("Http :%s, err:%d", ptr->url, dial_result->err);

        free(dial_result);
    }
    cJSON_AddItemToObject(result, "httpList", arr);
    post_data = cJSON_Print(result);
    if(Debug_mode()){
        log_trace("HTTP post_data:%s", post_data);
    }
    // data_report(http->kafka_url, topic, post_data, strlen(post_data));
    free(post_data);
    // cJSON_Delete(result);
    // pthread_cond_signal(&http->cond);
    // pthread_mutex_unlock(&http->mutex);
    return (void*)result;
}